package com.newyulong.implatform.db;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.newyulong.implatform.bean.UserInfoJsonBean;

public class UserDao {

	DbHelper helper = null;

	public UserDao(Context context) {
		helper = new DbHelper(context);
	}

	// 添加景点
	public void insertUserInfo(UserInfoJsonBean bean) {
		String sql = "insert into user_info (user_name,user_pas,rember_date)values(?,?,?)";
		SQLiteDatabase db = helper.getWritableDatabase();
		Date date = new Date(Calendar.getInstance().getTimeInMillis());
		db.execSQL(sql,new Object[] { bean.getUserName(),bean.getPassword(),
						date });
		db.close();
	}
	
	/**
	 * 得到用户信息
	 * @param userName
	 * @return
	 */
	public UserInfoJsonBean getUserInfoByName(String userName){
		SQLiteDatabase db = helper.getWritableDatabase();
		UserInfoJsonBean bean = null;
		String sql = "select * from user_info where user_name = ?";
		Cursor c = db.rawQuery(sql, new String[]{userName});
		while (c.moveToNext()) {
			bean = new UserInfoJsonBean();
			bean.setUserName(userName);
			bean.setPassword(c.getString(c.getColumnIndex("user_pas")));
		}
		c.close();
		return bean;
	}

	// 得到所有的景点
	public ArrayList<UserInfoJsonBean> getAllPoints() {
		String sql = "select * from points";
		SQLiteDatabase db = helper.getWritableDatabase();
		ArrayList<UserInfoJsonBean> pointList = new ArrayList<UserInfoJsonBean>();
		UserInfoJsonBean bean = null;
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			
		}
		cursor.close();
		db.close();
		return pointList;
	}
	
	public void deleteAllUser(){
		String sql = "delete from user_info";
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(sql);
		db.close();
	}

	public UserInfoJsonBean getUserInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		UserInfoJsonBean bean = null;
		String sql = "select * from user_info";
		Cursor c = db.rawQuery(sql,null);
		while (c.moveToNext()) {
			bean = new UserInfoJsonBean();
			bean.setUserName(c.getString(c.getColumnIndex("user_name")));
			bean.setPassword(c.getString(c.getColumnIndex("user_pas")));
		}
		c.close();
		db.close();
		return bean;
		
	}
	

}
